// reducers/cart.ts
import { CartState, CartAction } from '../types';
 
// 初始状态
const initialCartState: CartState = {
  items: [],
  total: 0,
};
 
// Cart Reducer
export const cartReducer = (
  state: CartState = initialCartState,
  action: CartAction
): CartState => {
  switch (action.type) {
    case 'cart/addItem':
      return {
        items: [...state.items, action.payload],
        total: state.items.length + 1,
      };
    case 'cart/clear':
      return initialCartState;
    default:
      return state;
  }
};